home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 49 / Amiga Format CD49 (2000-01-17)(Future Publishing)(GB)(Track 1 of 3)[!][issue 2000-02].iso / -serious- / comms / other / novia / src / novia_visualeditor.c < prev    next >
Text File  |  1999-12-06  |  15KB  |  683 lines

  1. #include <exec/lists.h>
  2. #include <pragma/dos_lib.h>
  3. #include <pragma/exec_lib.h>
  4. #include <novia/novia_PortData.h>
  5. #include <novia/novia_types.h>
  6. #include <novia/novia_misc.h>
  7. //#include <novia/novia_funcs.h>
  8. #include <exec/memory.h>
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. #include <string.h>
  12. #include <pragma/noviasys_lib.h>
  13.  
  14. struct line
  15. {
  16.     line  *ln_Succ;
  17.     line  *ln_Pred;
  18.     char  *text;
  19.     UWORD textsize;        // real length of line
  20.     UWORD buffersize;        // x bytes allocated memory
  21.     char    linefeed;
  22. };
  23.  
  24. struct linelist
  25. {
  26.     struct line *    lh_Head;
  27.     struct line *    lh_Tail;
  28.     struct line *    lh_TailPred;
  29. };
  30.  
  31.  
  32. /***********************************************************************/
  33.  
  34. struct line *VE_AllocateLine(ULONG len);
  35. void VE_WriteLine(line *line);
  36. void VE_FreeLine(struct line *freeline);
  37.  
  38. struct line *VE_AllocateLine(ULONG len)
  39. {
  40.     struct line *newline;
  41.     if ((newline = (line *)AllocVec(sizeof(line), MEMF_ANY | MEMF_CLEAR)))
  42.     {
  43.         if ((newline->text = (char *)AllocVec(len+1, MEMF_ANY | MEMF_CLEAR)))
  44.             newline->buffersize = len;
  45.         else
  46.         {
  47.             FreeVec(newline);
  48.             newline=NULL;
  49.         }
  50.     }
  51.     return newline;
  52. }
  53.  
  54.  
  55. void VE_WriteLine(line *line)
  56. {
  57.     Writeio(line->text,line->textsize);
  58.     if (line->text[line->textsize-1]!=10)
  59.         LF();
  60. }
  61.  
  62.  
  63. void VE_FreeLine(struct line *freeline)
  64. {
  65.     Remove((Node *)freeline);
  66.     FreeVec(freeline->text);
  67.     FreeVec(freeline);
  68. }
  69.  
  70. ULONG VisualEditor(char *filename, APTR data, ULONG flags)
  71. {
  72.     struct PortData *cport=(PortData *)FindTask(NULL)->tc_UserData;
  73.  
  74.     if (cport)
  75.     {
  76.         struct line *lineptr,        // actual edited line
  77.                         *newline,        // 
  78.                         *startline,        // first line in display
  79.                         *endline;        // last line in display
  80.  
  81.         struct linelist llist;
  82.         struct List mylist;
  83.  
  84.         ULONG lines        = 0;
  85.         ULONG textsize = 0;
  86.         char cp[20];
  87.         char    listnew    = TRUE;
  88.         char    quit        = FALSE;
  89.         char    *buffer    = (char *)AllocVec(1024,MEMF_ANY);
  90.         ULONG    ulong;
  91.         char    oldX;
  92.         char    result    = TRUE;
  93.         ULONG CurserX;
  94.         ULONG CurserY;
  95.         ULONG ystart    = 1;
  96.         ULONG    yend        = 1;
  97.         NewList((List *)&llist);
  98.  
  99.         if (flags & VISUALEDIT_FILE)
  100.         {
  101.             BPTR fh;
  102.             if (buffer && ((fh = Open(filename,MODE_OLDFILE))))
  103.             {
  104.                 ioprintf("reading file: %s\n",filename);
  105.                 while (!quit && FGets(fh, buffer, 1024))
  106.                 {
  107.                     ulong = mystrlen(buffer);
  108.                     if ((lineptr = VE_AllocateLine(ulong)))
  109.                     {
  110.                         CopyMem(buffer, lineptr->text, ulong);
  111.                         lineptr->textsize = ulong;
  112.                         if (buffer[ulong] == 10 | buffer[ulong] == 13)
  113.                         {
  114.                             lineptr->linefeed = TRUE;
  115.                             textsize++;
  116.                         }
  117.                         AddTail((List *)&llist, (Node *)lineptr);
  118.                         lines++;
  119.                         textsize = textsize + lineptr->textsize;
  120.                         if (lines <= 20)
  121.                         {
  122.                             endline    = lineptr;
  123.                             yend        = lines;
  124.                         }
  125.                     }
  126.                     else
  127.                     {
  128.                         ioprintf("unknown error. -125.99994582 \ncall Mircosoft Hotline (1)900-888-88888 ($3.99/min)\n");
  129.                         quit = TRUE;
  130.                     }
  131.                 }
  132.                 Close(fh);
  133.             }
  134.             else
  135.             {
  136.                 if (flags & VISUALEDIT_EXITIFNOTEXIT)
  137.                 {
  138.                     ioprintf("Can't open %s\n",filename);
  139.                     Writeio("Usage: EDITFILE filename\n",-1);
  140.                     if (!buffer)
  141.                     {
  142.                         ioprintf("Can't open File, not anouth memory\n");
  143.                     }
  144.                     quit=TRUE;
  145.                 }
  146.                 else
  147.                 {
  148.                     if ((lineptr = VE_AllocateLine(20)))
  149.                     {
  150.                         AddTail((List *)&llist, (Node *)lineptr);
  151.                         startline = lineptr;
  152.                         lines++;
  153.                         endline    = lineptr;
  154.                         yend        = lines;
  155.                     }
  156.                 }
  157.             }
  158.         }
  159.         else
  160.         {
  161.             if (flags & VISUALEDIT_FROMADDRESS)
  162.             {
  163.                 if (data)
  164.                 {
  165.                     char *ptr = data;
  166.                     quit = FALSE;
  167.                     while (!quit && *ptr)
  168.                     {
  169.                         ulong = mystrlen(ptr);
  170.                         if ((lineptr = VE_AllocateLine(ulong)))
  171.                         {
  172.                             CopyMem(ptr, lineptr->text, ulong);
  173.                             lineptr->textsize = ulong;
  174.                             ptr = ptr + ulong;
  175.                             if (ptr[ulong] == 10 | ptr[ulong] == 13)
  176.                             {
  177.                                 lineptr->linefeed = TRUE;
  178.                                 textsize++;
  179.                             }
  180.                             while (*ptr && (*ptr == 10 | *ptr == 13))
  181.                                 ptr++;
  182.                             AddTail((List *)&llist, (Node *)lineptr);
  183.                             lines++;
  184.                             textsize = textsize + lineptr->textsize;
  185.                             if (lines <= 20)
  186.                             {
  187.                                 endline    = lineptr;
  188.                                 yend        = lines;
  189.                             }
  190.                         }
  191.                         else
  192.                             quit = TRUE;
  193.                     }
  194.                 }
  195.             }
  196.             else
  197.                 quit = TRUE;
  198.         }
  199.      
  200.         if (!quit)
  201.         {
  202.             CLS();
  203.             ioprintf("z4c7 NOViA ViSUAL TEXT EDITOR                               V0.1beta               z0\n");
  204.             ioprintf("z4c7 [ESC]ape Functionsmenu                                                        z0\n");
  205.             CurserX    = 1;
  206.             CurserY    = 1;
  207.             oldX        = 1;
  208.             lineptr    = llist.lh_Head;
  209.             startline= llist.lh_Head;
  210.         }
  211.         while(!quit && !cport->ProgramClose &&  cport->network.online)
  212.         {
  213.             if (listnew)
  214.             {
  215.                 Writeio("HB",-1);    // Jump to textbeginn. X=1; Y=3
  216.                 ulong        = 0;
  217.                 newline    = lineptr;
  218.                 while (newline->ln_Succ && ulong<20)
  219.                 {
  220.                     VE_WriteLine(newline);
  221.                     newline = newline->ln_Succ;
  222.                     ulong++;
  223.                 }
  224.                 Writeio("HB",-1);    // Jump to textbeginn. X=1; Y=3            
  225.                 listnew = FALSE;
  226.             }
  227.  
  228.  
  229.             Conread(cp,1,0);
  230.  
  231.         /*** RETURN ***/
  232.  
  233.             if (*cp == 13 | *cp == 10)
  234.             {
  235.                 if ((newline = VE_AllocateLine(80)))
  236.                 {
  237.                     newline->textsize                = lineptr->textsize - CurserX + 1;
  238.                     newline->linefeed                = lineptr->linefeed;
  239.  
  240.                     if (newline->textsize)
  241.                         CopyMem(&lineptr->text[CurserX - 1], newline->text, newline->textsize);    // Copy from Curser X to end of line
  242.                     
  243.                     lineptr->text[CurserX - 1]    = 0;                        // Insert LineFeed;
  244.                     lineptr->textsize                = CurserX - 1;
  245.                     lineptr->linefeed                = TRUE;
  246.                     textsize++;
  247.  
  248.                     Insert((List *)&llist, (Node *)newline, (Node *)lineptr);
  249.                     lineptr    = newline;
  250.                     if (CurserY < 15) // && yend < lines)
  251.                     {
  252.                         ioprintf("HM%dHL%s\r", CurserY + 4, lineptr->text);
  253.                         CurserY++;
  254.                         if (yend < 20)
  255.                         {
  256.                             yend++;
  257.                             endline = (line *)llist.lh_TailPred;
  258.                         }
  259.                         else
  260.                         {
  261.                             if (endline->ln_Pred->ln_Pred)
  262.                                 endline = endline->ln_Pred;
  263.                         }
  264.                     }
  265.                     else
  266.                     {
  267.                         if (yend < 20)
  268.                         {
  269.                             ioprintf("EL%s", lineptr->text);
  270.                             endline = (line *)llist.lh_TailPred;
  271.                             CurserY++;
  272.                         }
  273.                         else
  274.                         {
  275.                             if (startline->ln_Succ->ln_Succ)
  276.                                 startline = startline->ln_Succ;
  277.                             if (endline->ln_Succ->ln_Succ)
  278.                                 endline = endline->ln_Succ;
  279.                             ystart++;
  280.                             ioprintf("HM%dHL%s\r", CurserY + 3, lineptr->text);
  281.                         }
  282.                         yend++;
  283.                     }
  284.                     CurserX    = 1;
  285.                     oldX        = 1;
  286.                     lines++;
  287.                 }
  288.             }
  289.  
  290.         /*** [ESC]ape ***/
  291.  
  292.             if (*cp==27)
  293.             {
  294.                     Writeio("Hz4c7 [Q]uit, [O]pen, [s]ave, [S]ave as, [F]ind/search                     z0\n",-1);
  295.                 Conread(cp, 1, 0);
  296.                 switch(*cp)
  297.                 {
  298.                     case 'Q': case 'q':
  299.                         quit = TRUE;
  300.                         CLS();
  301.                         break;
  302.                     case 's':
  303.                         CLS();
  304.                         {
  305.                             BPTR fh;
  306.                             if ((fh = Open(filename, MODE_NEWFILE)))
  307.                             {
  308.                                 newline    = llist.lh_Head;
  309.                                 quit        = FALSE;
  310.                                 while(newline->ln_Succ && !(quit = FPuts(fh, newline->text)))
  311.                                 {
  312.                                     newline = newline->ln_Succ;
  313.                                 }
  314.                                 if (quit)PrintDosError(IoErr());
  315.                                 Close(fh);
  316.                             }
  317.                             else
  318.                                 PrintDosError(IoErr());
  319.                             quit = TRUE;
  320.                         }
  321.                         break;
  322.                     case 'S':
  323.                         CLS();
  324.                         {
  325.                             BPTR fh;
  326.                             if ((fh = Open("ram:test", MODE_NEWFILE)))
  327.                             {
  328.                                 newline    = llist.lh_Head;
  329.                                 quit        = FALSE;
  330.                                 while(newline->ln_Succ && !(quit = FPuts(fh, newline->text)))
  331.                                 {
  332.                                     newline = newline->ln_Succ;
  333.                                 }
  334.                                 if (quit)PrintDosError(IoErr());
  335.                                 Close(fh);
  336.                             }
  337.                             else
  338.                                 PrintDosError(IoErr());
  339.                             quit=TRUE;
  340.                         }
  341.                         break;                    
  342.                 }
  343.                 if (!quit)
  344.                     Writeio("Hz4c7 [ESCape] Functionsmenu                                               z0\n",-1);
  345.             }
  346.  
  347.         /*** [BACKSPACE] ***/
  348.  
  349.             if (*cp == 8)
  350.             {
  351.                 if (CurserX > 1)
  352.                 {
  353.                     int i;
  354.                     Writeio(cp,1);
  355.                     CurserX--;
  356.                     oldX = CurserX;
  357.                     for (i = CurserX - 1; i < lineptr->textsize - 1; i++)
  358.                     {
  359.                         lineptr->text[i] = lineptr->text[i+1];
  360.                     }
  361.                     lineptr->textsize--;
  362.                     lineptr->text[lineptr->textsize] = 0;
  363.                     textsize--;
  364.                     Writeio("P",-1);
  365.                 }
  366.             }
  367.  
  368.         /*** [DEL]ete ***/
  369.  
  370.             if (*cp == 127)
  371.             {
  372.                 if (CurserX <= lineptr->textsize)
  373.                 {
  374.                     int i;
  375.                     for (i = CurserX - 1; i < lineptr->textsize - 1; i++)
  376.                     {
  377.                         lineptr->text[i] = lineptr->text[i + 1];
  378.                     }
  379.                     lineptr->textsize--;
  380.                     lineptr->text[lineptr->textsize] = 0;
  381.                     textsize--;
  382.                     Writeio("P",-1);
  383.                 }
  384.             }
  385.  
  386.         /*** [CTRL] - [D], Kill to end of line ***/
  387.  
  388.             if (*cp == 4)
  389.             {
  390.                 if (CurserX <= lineptr->textsize)
  391.                 {
  392.                     textsize = textsize - (lineptr->textsize - (CurserX - 1));
  393.                     lineptr->text[CurserX - 1]    = 0;
  394.                     lineptr->textsize                = CurserX - 1;
  395.                     Writeio("",-1);
  396.                 }
  397.             }
  398.  
  399.         /*** [CTRL] - [K], delete line ***/
  400.  
  401.             if (*cp == 11)
  402.             {
  403.                 if (lineptr->ln_Succ->ln_Succ)
  404.                 {
  405.                     CurserX    = 1;
  406.                     oldX        = CurserX;
  407.     
  408.                     Writeio("M",-1);
  409.                     newline = lineptr->ln_Succ; 
  410.  
  411.                     if (startline == lineptr)
  412.                         startline = newline;
  413.  
  414.                     if (lines - yend > 20)
  415.                     {
  416.                         if (endline->ln_Succ->ln_Succ)
  417.                         {
  418.                             endline = endline->ln_Succ;
  419.                             ioprintf("H%s%dH",endline->text,CurserY + 3);
  420.                         }
  421.                     }
  422.                     else
  423.                         yend--;
  424.  
  425.                     textsize = textsize - lineptr->textsize;
  426.                     if (lineptr->linefeed)
  427.                         textsize--;
  428.                     VE_FreeLine(lineptr);
  429.                     lineptr = newline;
  430.                     lines--;
  431.                 }
  432.             }
  433.  
  434.         /*** ASCII KEYS ***/
  435.  
  436.             if (isgraph(*cp) | *cp == ' ' && lineptr->textsize < 80) 
  437.             {
  438.                 if (lineptr->textsize + 3 > lineptr->buffersize)
  439.                 {
  440.                     char *newtext = (char *) AllocVec(lineptr->textsize +20 , MEMF_ANY | MEMF_CLEAR);
  441.                     if (newtext)
  442.                     {
  443.                         CopyMem(lineptr->text, newtext, lineptr->textsize);
  444.                         FreeVec(lineptr->text);
  445.                         lineptr->text = newtext;
  446.                         lineptr->buffersize = lineptr->textsize + 20;
  447.                     }
  448.                 }
  449.                 if (lineptr->textsize + 1 < lineptr->buffersize)
  450.                 {
  451.                     if (CurserX == lineptr->textsize + 1)
  452.                     {
  453.                         Writeio(cp,1);
  454.                         lineptr->text[lineptr->textsize]        = *cp;
  455.                         lineptr->text[lineptr->textsize+1]    = 0;
  456.                         lineptr->textsize++;
  457.                         CurserX++;
  458.                         oldX = CurserX;
  459.                     }
  460.                     else
  461.                     {
  462.                         Writeio("@",-1);
  463.                         Writeio(cp,1);
  464.                         memmove(&lineptr->text[CurserX], &lineptr->text[CurserX - 1], (lineptr->textsize - CurserX) + 1);
  465.                         lineptr->text[CurserX - 1] = *cp;
  466.                         lineptr->text[lineptr->textsize + 1] = 0;
  467.                         lineptr->textsize++;
  468.                         CurserX++;
  469.                         oldX = CurserX;
  470.                     }
  471.                     textsize++;
  472.                 }
  473.             }
  474.             if (*cp==-101)                        // beginning Curser Keys
  475.             {
  476.                 Conread(cp,1,0);
  477.                 switch (*cp)
  478.                 {
  479.  
  480.         /*** CURSER LEFT ***/
  481.  
  482.                     case 'D':
  483.                         if (CurserX > 1)
  484.                         {
  485.                             Writeio("D",-1);
  486.                             CurserX--;
  487.                         }
  488.                         else
  489.                         {
  490.                             if (lineptr->ln_Pred->ln_Pred)
  491.                             {
  492.                                 lineptr = lineptr->ln_Pred;
  493.                                 if (CurserY <= 5)
  494.                                 {
  495.                                     if (startline->ln_Pred->ln_Pred)
  496.                                     {
  497.                                         startline    = startline->ln_Pred;
  498.                                         Writeio("HMHL",-1);
  499.                                         VE_WriteLine(startline);
  500.                                         sprintf(buffer,"%dH",CurserY + 3);
  501.                                         Writeio(buffer,-1);
  502.                                         yend--;
  503.                                         endline = endline->ln_Pred;
  504.                                     }
  505.                                     else
  506.                                     {
  507.                                         LF();
  508.                                         Writeio("A",-1);
  509.                                         CurserY--;
  510.                                     }
  511.                                 }
  512.                                 else
  513.                                 {
  514.                                     LF();
  515.                                     Writeio("A",-1);
  516.                                     CurserY--;
  517.                                 }
  518.                                 if (lineptr->textsize)
  519.                                 {
  520.                                     cright(lineptr->textsize);
  521.                                     CurserX = lineptr->textsize + 1;
  522.                                 }
  523.                                 else
  524.                                     CurserX = 1;
  525.                             }
  526.                         }
  527.                         oldX = CurserX;
  528.                         break;
  529.  
  530.         /*** CURSER RIGHT ***/
  531.  
  532.                     case 'C':
  533.                         if (CurserX <= lineptr->textsize)
  534.                         {
  535.                             Writeio("C",-1);
  536.                             CurserX++;
  537.                         }
  538.                         else
  539.                         {
  540.                             if (lineptr->ln_Succ->ln_Succ)
  541.                             {
  542.                                 lineptr = lineptr->ln_Succ;
  543.                                 if (CurserY >= 15)
  544.                                 {
  545.                                     if (endline->ln_Succ->ln_Succ)
  546.                                     {
  547.                                         startline    = startline->ln_Succ;
  548.                                         endline        = endline->ln_Succ;
  549.                                         Writeio("HMH",-1);
  550.                                         VE_WriteLine(endline);
  551.                                         sprintf(buffer,"%dH", CurserY + 3);
  552.                                         Writeio(buffer,-1);
  553.                                         yend++;
  554.                                     }
  555.                                     else
  556.                                     {
  557.                                         LF();
  558.                                         CurserY++;
  559.                                     }
  560.                                 }
  561.                                 else
  562.                                 {
  563.                                     LF();
  564.                                     CurserY++;
  565.                                 }
  566.                                 CurserX = 1;
  567.                             }
  568.                         }
  569.                         oldX = CurserX;
  570.                         break;
  571.  
  572.         /*** CURSER UP ***/
  573.  
  574.                     case 'A':
  575.                         if (lineptr->ln_Pred->ln_Pred)
  576.                         {
  577.                             lineptr = lineptr->ln_Pred;
  578.                             if (CurserY <= 5)
  579.                             {
  580.                                 if (startline->ln_Pred->ln_Pred)
  581.                                 {
  582.                                     startline    = startline->ln_Pred;
  583.                                     if (yend > 20)
  584.                                         endline        = endline->ln_Pred;
  585.                                     Writeio("HMHL",-1);
  586.                                     VE_WriteLine(startline);
  587.                                     sprintf(buffer,"%dH",CurserY + 3);
  588.                                     Writeio(buffer,-1);
  589.                                     yend--;
  590.                                     ystart--;
  591.                                 }
  592.                                 else
  593.                                 {
  594.                                     Writeio("F",-1);
  595.                                     CurserY--;
  596.                                 }
  597.                             }
  598.                             else
  599.                             {
  600.                                 Writeio("F",-1);
  601.                                 CurserY--;
  602.                             }
  603.                             if (oldX > lineptr->textsize)
  604.                             {
  605.                                 if (lineptr->textsize)
  606.                                     cright(lineptr->textsize);
  607.                                 CurserX = lineptr->textsize + 1;
  608.                             }
  609.                             else
  610.                             {
  611.                                 if (oldX > 1)
  612.                                     cright(oldX - 1);
  613.                                 CurserX = oldX;
  614.                             }
  615. //                            printf("Hlineptr: %-80s\n",lineptr->text);
  616.                         }
  617.                         break;
  618.  
  619.         /*** CURSER DOWN ***/
  620.  
  621.                     case 'B':
  622.                         if (lineptr->ln_Succ->ln_Succ)
  623.                         {
  624.                             lineptr = lineptr->ln_Succ;
  625.                             if (CurserY >= 15)
  626.                             {
  627.                                 if (endline->ln_Succ->ln_Succ)
  628.                                 {
  629.                                     startline    = startline->ln_Succ;
  630.                                     endline        = endline->ln_Succ;
  631.                                     Writeio("HMH",-1);
  632.                                     VE_WriteLine(endline);
  633.                                     sprintf(buffer,"%dH",CurserY + 3);
  634.                                     Writeio(buffer,-1);
  635.                                     yend++;
  636.                                     ystart++;
  637.                                 }
  638.                                 else
  639.                                 {
  640.                                     Writeio("E",-1);
  641.                                     CurserY++;
  642.                                 }
  643.                             }
  644.                             else
  645.                             {
  646.                                 Writeio("E",-1);
  647.                                 CurserY++;
  648.                             }
  649.                             if (oldX > lineptr->textsize)
  650.                             {
  651.                                 if (lineptr->textsize)
  652.                                     cright(lineptr->textsize);
  653.                                 CurserX = lineptr->textsize + 1;
  654.                             }
  655.                             else
  656.                             {
  657.                                 if (oldX > 1)
  658.                                     cright(oldX - 1);
  659.                                 CurserX = oldX;
  660.                             }
  661. //                            printf("Hlineptr: %-80s\n",lineptr->text);
  662.                         }
  663.                         break;
  664.                 }
  665.             }
  666. //            ioprintf("pHz4c7 X: %02d Y: %02d lines: %04d (%02d:%02d) sl: <%s>\n                                    pt: <%s>\n                                    el: <%s>z0%d;%dHp",CurserX,CurserY,lines,ystart,yend,startline->text,lineptr->text,endline->text,CurserY + 3,CurserX);
  667.             ioprintf("pHz4c7 X: %02d Y: %02d lines: %04d ystart: %04d yend: %04d textsize: %05dz0%d;%dHp",CurserX,CurserY,lines,ystart,yend,textsize,CurserY + 3,CurserX);
  668.         }
  669.         if (buffer)FreeVec(buffer);
  670.         while (!IsListEmpty((List *)&llist))VE_FreeLine(llist.lh_Head);
  671.         return result;
  672.     }
  673.     
  674. }
  675.  
  676.  
  677. int mystrlen(char *str)
  678. {
  679.     char *ptr = str;
  680.     while (*ptr != 0 && *ptr != 10 && *ptr != 13)
  681.         ptr++;
  682.     return(ptr - str);
  683. }